package com.hs.bai.controller;

import com.hs.bai.pojo.Addrinfo;
import com.hs.bai.pojo.Expenseinfo;
import com.hs.bai.pojo.Order;
import com.hs.bai.pojo.Orderbasicinfo;
import com.hs.bai.service.IAddrinfoService;
import com.hs.bai.service.IExpenseinfoService;
import com.hs.bai.service.IOrderService;
import com.hs.bai.service.IOrderbasicinfoService;
import com.hs.dto.Constants;
import com.hs.dto.ResultBean;
import com.hs.zhou.pojo.Bill;
import com.hs.zhou.service.IBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 创建订单基本信息 前端控制器
 * </p>
 *
 * @author Dingkun Bai
 * @since 2022-06-13
 */
@CrossOrigin
@RestController
@RequestMapping("/orderbasicinfo")
@Slf4j
@Api(tags = "订单基本信息模块")
public class OrderbasicinfoController {

    @Autowired
    private IOrderbasicinfoService orderBasicInfoService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IAddrinfoService addrinfoService;
    @Autowired
    private IExpenseinfoService expenseinfoService;
    @Autowired
    private IBillService billService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/find/{id}")
    public ResultBean findById(@PathVariable String id){
        Orderbasicinfo orderbasicinfo = orderBasicInfoService.getById(id);
        ResultBean ok = ResultBean.OK();
        ok.setData(orderbasicinfo);
        return ok;
    }

    /**
     * 添加订单基本信息
     * @param orderbasicinfo
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加订单基本信息")
    @Transactional
    public ResultBean addBasicOrderInfo(@RequestBody Orderbasicinfo orderbasicinfo){
        if (orderbasicinfo != null) {

            log.info("=================={}",orderbasicinfo.toString());

            //查询订单总表是否存在当前订单
            String id = orderbasicinfo.getId();
            Order order = orderService.getById(id);
            //已有当前订单
            ResultBean idBean = new ResultBean();
            if (order != null) {
                return ResultBean.ERROR("添加失败！");
            } else {

                //增加t_bill
                Bill bill = new Bill();
                bill.setOrderid(id);
                bill.setCustomerName(orderbasicinfo.getCustomerName());
                billService.save(bill);

                //增加订单
                Order newOrder = new Order();
                newOrder.setId(id);
                newOrder.setOrderType(orderbasicinfo.getOrderType());
                newOrder.setTransType(orderbasicinfo.getTransType());
                newOrder.setCustomerName(orderbasicinfo.getCustomerName());
                newOrder.setOrderTime(orderbasicinfo.getReceiptDate());
                log.info("=================={}",orderbasicinfo.gettRId());
                newOrder.setTRId(orderbasicinfo.gettRId());
                //默认为未提交
                newOrder.setOrderStatus(0);

                //生成地址信息、费用信息编号
                int addId = 0;
                int feeId = 0;
                boolean flag;
                do {
                    addId = (int) (Math.random() * 9000 + 1000);
                    feeId = (int) (Math.random() * 9000 + 1000);
                    //防止编号重复
                    Addrinfo addrinfo = addrinfoService.getById(addId);
                    Expenseinfo expenseinfo = expenseinfoService.getById(feeId);
                    flag = addrinfo == null && expenseinfo == null;
                }while (!flag);

//                int goodsId = (int) (Math.random() * 9000 + 1000);

                newOrder.setTAId(addId);
                newOrder.setTFId(feeId);
//                newOrder.setTGId(goodsId);
                orderService.save(newOrder);
                List<Integer> ids = new ArrayList<>();
                ids.add(addId);
                ids.add(feeId);
//                ids.add(goodsId);
                idBean.setData(ids);
                idBean.setCode(Constants.SUCCESS_CODE);
                idBean.setMsg("添加成功！");
            }
            boolean b = orderBasicInfoService.save(orderbasicinfo);
            return b ? idBean : ResultBean.ERROR("添加失败！");
        }
        return ResultBean.ERROR("添加失败！");
    }

    /**
     * 修改订单基本信息
     * @param orderbasicinfo
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("添加订单基本信息")
    @Transactional
    public ResultBean updateBasicOrder(@RequestBody Orderbasicinfo orderbasicinfo){

            Order order = orderService.getById(orderbasicinfo.getId());
             //增加订单
            Order newOrder = new Order();
            newOrder.setId(orderbasicinfo.getId());
            newOrder.setOrderType(orderbasicinfo.getOrderType());
            newOrder.setTransType(orderbasicinfo.getTransType());
            newOrder.setCustomerName(orderbasicinfo.getCustomerName());
            newOrder.setOrderTime(orderbasicinfo.getReceiptDate());
            newOrder.setTRId(orderbasicinfo.gettRId());
            //默认为未提交
            newOrder.setOrderStatus(order.getOrderStatus());
            newOrder.setTAId(order.getTAId());
            newOrder.setTFId(order.getTFId());
            boolean b1 = orderService.updateById(newOrder);
            boolean b = orderBasicInfoService.updateById(orderbasicinfo);
            return b1&&b ? ResultBean.OK("修改成功！"):ResultBean.ERROR("修改失败!");

    }


}
